<?php
/**
 * @author Eugenio Fage
 * http://eugeniofage.wordpress.com/
 * http://code.google.com/p/monky/
 * 
 */
class Vote extends AppModel {
	var $name = 'Vote';

	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Link' => array(
			'className' => 'Link',
			'foreignKey' => 'link_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Comment'=>array(
			'className' => 'Comment',
			'foreignKey' => 'link_id',
		),
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	function getVoteFreq($user){
		$conditions=array(
			'type'=>'links',
			'Vote.created > subtime(now(), \'0:0:30\')'		
		);
		
		if(!$user['User']['id']){
			$conditions['ip']=Visitor::ip_int();
			$user['User']['id']=0;	
		}
		
		$conditions['user_id']=$user['User']['id'];
		
		return $this->find('count',array('conditions'=>$conditions,'recursive'=>-1));
	}
	
	function getVoteCommentFreq($user){
		extract($user['User']);
		$conditions=array(
			'type'=>'links',
			'Vote.created > subtime(now(), \'0:0:30\')'		
		);
		
		if(!$id){
			$conditions['ip']=Visitor::ip_int();
			$id=0;	
		}
		
		$conditions['user_id']=$id;
		$recursive=-1;
		
		return $this->find('count',compact('conditions','recursive'));
	}
	
	function getLinksVoteValue($linksGetVoteValue,$user_id){
		
		if($user_id){
			$conditions['AND']['OR']=array('Vote.user_id'=>$user_id,'Vote.ip'=>Visitor::ip_int());
		}else{
			$conditions['Vote.user_id']=0;
			$conditions['Vote.ip']=Visitor::ip_int();
		}	
		$conditions['Vote.link_id']=array_values($linksGetVoteValue);
				
		$valueList=$this->find('list',array(
			'conditions'=>$conditions,
			'fields'=>array('Vote.link_id','Vote.value')
		));
				
		$linksGetVoteValue = array_flip($linksGetVoteValue);
		$values=array();
		
		foreach (array_values($linksGetVoteValue) as $pos){
			$values[$pos]=0;	
		}
		
		foreach ($valueList as $link_id=>$value){
			$values[$linksGetVoteValue[$link_id]]=$value;
		}
		
		return $values;
	}
		
	function getVoteValue($link_id,$user_id=null,$type='links',$check_ip=false){
		$conditions=array('Vote.type'=>$type,'Vote.link_id'=>$link_id);
				
		if ($check_ip || !$user_id) $conditions['Vote.ip']=Visitor::ip_int();
				
		if($user_id){
			$conditions['AND']['OR']=array('Vote.user_id'=>$user_id,'Vote.ip'=>Visitor::ip_int());
		}else{
			$conditions['Vote.user_id']=0;
		}	
		
		$fields=array('Vote.value','Vote.id');
		$recursive=-1;
		
		$vote=$this->find('first',compact('fields','conditions','recursive'));
		
		if(empty($vote)){
			return false;
		}		
		return $vote['Vote']['value'];
	}
			
	function userHasVoteLink($link_id,$user_id,$check_ip=false){	
		return !($this->getVoteValue($link_id,$user_id,'links',$check_ip)===false);
	}
	
	function userHasVoteComment($comment_id,$user_id,$check_ip=false){
		return !($this->getVoteValue($comment_id,$user_id,'comments',$check_ip)===false);
	}
	
	function countLinkVoteClons($link_id,$user_id,$days){
		return $this->countClons('links', $link_id, $user_id,'Vote.user_id',$days);	
	}
	
	
	function countClons($type,$link_id,$user_id,$to,$days=7){
		$c = $this->find('count',array(
			'conditions'=>array(
				'Vote.type'=>$type,
				'Vote.link_id' => $link_id,
			),'joins'=>array(
				array(
					'type'=>'left',
					'table' => 'clons',
            		'alias' => 'Clon',
            		'conditions'=> array(
						'Clon.from' => $user_id,
						'Clon.to' => $to, 
						'Clon.created > date_sub(now(), interval '.$days.' day)'
					)				
				)
			)
		));
		
		return $c;
	}
	
	/**
	 * 
	 * set the values to create a vote
	 * @param $user
	 */
	function setVoteData($data){
				
		if($data['User']['vote']<0){
			$data['User']['vote_type']='negative';
		}else{
			$data['User']['vote_type']='positive';
		}

		if($data['User']['id']==0){
			$data['User']['karma']=round(Configure::read('Monky.Anon.karma'),2);
		}else{
			$data['User']['karma']=$this->User->field('karma',array('id'=>$data['User']['id']));
			if($data['User']['karma']<0) $data['User']['karma']=1;	
		}
		if($data['User']['vote_type']=='positive'){
			$data['User']['vote']=$data['User']['karma'];
		}
				
		
		return $data;
	}
	
}
